<?php
require('log.php');
$start = microtime();

$id = $_GET['id'];

try {

    $connection = new MongoClient();
    $database   = $connection->selectDB('blog');
    $collection = $database->selectCollection('articles');

} catch(MongoConnectionException $e) {
    die("Failed to connect to database ".$e->getMessage());
}

$cursor  = $collection->find(array('_id' => new MongoId($id)));
$article = $cursor->getNext();
?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" href="bootstrap.css" />
        <link rel="stylesheet" href="style.css" />
        <title>MongoBeer</title>

    </head>

    <body>
    <div id="contentarea">
        <div id="innercontentarea">
            <h1><?php echo $article['title']; ?></h1>
            <p><?php echo $article['content']; ?></p>
            <div id="comment-section">
                <?php if (!empty($article['comments'])): ?>
                <h3>Commentaire</h3>

                <table id="tabComment">
                    <?php foreach($article['comments'] as $comment): ?>
                        <tr>
                            <td rowspan=2><img src="images/anonyme.jpg" /></td>
                            <td>
                                <?php echo '<b>'.$comment['name'].'</b>'; ?> le <?php echo date('d/m/y à h:i:s', $comment['posted_at']->sec);?>
                            </td>
                        </tr>
                        <tr>
                            <td><?php echo $comment['comment']; ?></td>
                        </tr>
                    <?php endforeach; endif;?>
                </table>

                <h3>Poster votre commentaire</h3>
                <br/>
                <form action="comment.php" method="post">
                    <span class="input-label">Pseudo</span>
                    <input type="text" name="commenter_name" class="comment-input"/><br/><br/>
                    <span class="input-label">Email</span>
                    <input type="text" name="commenter_email" class="comment-input"/><br/><br/>
                    <textarea name="comment" rows="5"></textarea><br/><br/>
                    <input type="hidden" name="article_id" value="<?php echo $article['_id']; ?>"/>
                    <input type="submit" name="btn_submit" value="Valider"/>
                    <input type="button" name="btn_cancer" value="Retour" onClick="history.go(-1)"/>
                </form>
            </div>
        </div>
    </div>
    </body>
    </html>

<?php

$end = microtime();
$data = array('response_time_ms' => ($end - $start) * 1000);

$logger = new Logger();
$logger->logRequest($data);
$logger->updateVisitCounter($id);

?>